package com.yami.shop.mystery.platform.task;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yami.shop.mystery.common.dto.UserInviteDto;
import com.yami.shop.mystery.common.model.ActivityPresell;
import com.yami.shop.mystery.common.model.ActivityProd;
import com.yami.shop.mystery.common.model.ActivityRule;
import com.yami.shop.mystery.common.model.InviteRecord;
import com.yami.shop.mystery.common.service.*;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 功能描述: 拉新活动商品发放
 *
 * @Author: LSR
 * @Date: 2023/8/30 15:18
 */

@Component
@AllArgsConstructor
public class InviteRecordTask {

    @Autowired
    private ActivityPresellService activityPresellService;
    @Autowired
    private ActivityRuleService activityRuleService;
    @Autowired
    private UserInviteService userInviteService;
    @Autowired
    private InviteRecordService inviteRecordService;
    @Autowired
    private ActivityProdService activityProdService;

    @XxlJob("inviteTask")
    public void inviteTask() {
        // 获取正在进行 的拉新活动 以及 活动规则
        QueryWrapper<ActivityPresell> activityPresellQueryWrapper = new QueryWrapper<>();
        activityPresellQueryWrapper.le("activity_start_time", new Date());
        activityPresellQueryWrapper.ge("activity_end_time", new Date());
        activityPresellQueryWrapper.eq("del_flag", 0);
        activityPresellQueryWrapper.eq("activity_type", 6);
        ActivityPresell one = activityPresellService.getOne(activityPresellQueryWrapper);
        if (ObjectUtil.isEmpty(one)) {
            return;
        }
        // 获取 活动规则
        QueryWrapper<ActivityRule> activityRuleQueryWrapper = new QueryWrapper<>();
        activityRuleQueryWrapper.in("rule_id", Arrays.asList(one.getActivityRule().split(",")));
        activityRuleQueryWrapper.in("condition_type", Arrays.asList("01", "03", "04"));
        ActivityRule activityRule = activityRuleService.getOne(activityRuleQueryWrapper);
        if (ObjectUtil.isEmpty(activityRule)) {
            return;
        }
        List<String> userList = new ArrayList<>();
        // 获取 符合 活动规则的人数 ------- 01大于02小于03等于04大于等于05小于等于
        List<UserInviteDto> userInviteDtoList = userInviteService.selectConformToRules(one.getId(), activityRule.getRuleType(), activityRule.getConditionNum(),one.getIsReal());
        for (UserInviteDto userInviteDto : userInviteDtoList) {
            // 获取 发放奖励个数
            long count = inviteRecordService.count(new QueryWrapper<InviteRecord>()
                    .eq("user_id", userInviteDto.getUserId())
                    .eq("activity_id", one.getId()));
            // 拉新人数 - 发放奖励个数 * 规则  如果 大于 规则数  添加 如 待空投 名单
            Long inviteNum = userInviteDto.getInviteNum() - count * activityRule.getConditionNum();
            if (inviteNum > activityRule.getConditionNum()) {
                // 剩余 拉新人数 / 规则数
                long grantNum = inviteNum / activityRule.getConditionNum();
                for (long i = grantNum; i > 0; i--) {
                    userList.add(userInviteDto.getUserId());
                }
            }
        }
        if (ObjectUtil.isEmpty(userList)) {
            return;
        }
        // 获取发放商品信息
        ActivityProd activityProd = activityProdService.getOne(new QueryWrapper<ActivityProd>().eq("activity_id", one.getId()));
        for (String userId : userList) {
            // 添加 奖励发放 记录
            InviteRecord inviteRecord = new InviteRecord();
            inviteRecord.setUserId(userId);
            inviteRecord.setProdId(activityProd.getProdId());
            inviteRecord.setActivityId(one.getId());
            inviteRecord.setStatus(0L);
            inviteRecordService.save(inviteRecord);
        }
    }
}
